<?php

namespace Core\Pay;

use Config\AliPayConfig;
class AliPay
{
    /**
     * 阿里云支付
     * entry $data
     * return json
     * */
    public static function aliPay($data){
        $aop = new \AopCertClient();
        $aop->gatewayUrl = AliPayConfig::GATEWAYURL;
        $aop->appId = AliPayConfig::APPID;
        $aop->rsaPrivateKey = AliPayConfig::RSAPRIVATEKEY;
        $aop->alipayrsaPublicKey = $aop->getPublicKey(AliPayConfig::ALIPAYCERTPATH);//调用getPublicKey从支付宝公钥证书中提取公钥
        $aop->apiVersion = AliPayConfig::APIVERSION;
        $aop->signType = AliPayConfig::SIGNTYPE;
        $aop->postCharset = AliPayConfig::POSTCHARSET;
        $aop->format = AliPayConfig::FORMAT;
        $aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书，如果开启校验要保证支付宝根证书在有效期内
        $aop->appCertSN = $aop->getCertSN(AliPayConfig::APPCERTPATH);//调用getCertSN获取证书序列号
        $aop->alipayRootCertSN = $aop->getRootCertSN(AliPayConfig::ROOTCERTPATH);//调用getRootCertSN获取支付宝根证书序列号
        $object = new \stdClass();
        $object->out_trade_no = $data["out_trade_no"];
        $object->total_amount = $data["total_amount"];
        $object->subject = $data["subject"];
        $object->product_code ='QUICK_WAP_WAY';
        $object->time_expire = $data["time_expire"];
        $json = json_encode($object);
        $request = new \AlipayTradeWapPayRequest();
        $request->setNotifyUrl($data["NotifyUrl"]);
        $request->setReturnUrl("http://h5.yiyacanyin.com/#/pages/PayResult/PayResult?type=支付宝 ".$data["total_amount"]);
        $request->setBizContent($json);
        return $aop->pageExecute ( $request);
    }
    public static function aliPayApp($data){
        $aop = new \AopClient();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = AliPayConfig::APPID;
        $aop->rsaPrivateKey = AliPayConfig::RSAPRIVATEKEY;
        $aop->alipayrsaPublicKey = AliPayConfig::ALIPAYCERTPATH;
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='GBK';
        $aop->format='json';
        $object = new \stdClass();
        $object->out_trade_no = $data["out_trade_no"];
        $object->total_amount = $data["total_amount"];
        $object->subject = $data["subject"];
        $object->product_code ='QUICK_MSECURITY_PAY';
        $object->time_expire = $data["time_expire"];
        $json = json_encode($object);
        $request = new \AlipayTradeAppPayRequest();
        $request->setNotifyUrl($data["NotifyUrl"]);
        $request->setBizContent($json);
        $result = $aop->sdkExecute($request);
        return $result;
        /*$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        $resultCode = $result->$responseNode->code;
        if(!empty($resultCode)&&$resultCode == 10000){
            echo "成功";
        } else {
            echo "失败";
        }*/
    }
}